#!/bin/bash

####位置自检
if [ ! $(command -v yunzai-menu) ];then
echo -e "$0: 脚本位置不在PATH环境，拒绝运行"
exit 1
fi

#-_-# 唯二依赖于yunzai且有正经功能的插件,维护难度较高...

####定位插件与母体
if [ ! $(command -v yunzai) ];then
echo -e "\033[31myunzai-menu: 未安装主体: yunzai，无法正常运行\033[0m"
exit 1
fi
if [ ! $(command -v yunzai-list) ];then
echo -e "\033[31mmyunzai-menu: 未安装插件: yunzai-list，无法正常运行\033[0m"
exit 1
fi

####接口清单
title=Yunzai管理插件™界面
versions=1.6.9
times=2023/06/26
main=( $(yunzai readme) )
name=${main[0]}
version=${main[1]}
time=${main[2]}
index=${main[3]}
plugin=${main[4]}

#标记bot是否存在
if [ -f $HOME/$name/lib/bot.js ] && [ -f $HOME/$name/app.js ];then
bot=ok
else
bot=no
fi
yunzai run inss dialog

####主模块
#回车键
back(){ echo '回车返回';read -s ENTER;}

#辅助窗口
inputbox(){ dialog --title "$title" --ok-button "确认" --cancel-button "返回" --inputbox "$1" 0 0 "$2" 2>&1 >/dev/tty;}

#Ctrl c对话框
close(){ choice=$(dialog --clear --title "$title" --yes-button "是的" --no-button "我再想想" --yesno "真的要退出吗" 10 30 2>&1 >/dev/tty)
if [ $? = 0 ];then
clear
exit 0
elif [ $? = 1 ];then
home
fi
}
trap close 2

list(){
choice=$(dialog --clear --title "$title" \
        --ok-button "确认" --cancel-button "返回" \
        --menu "查看插件" 10 30 2 \
        1 "查看js插件" \
        2 "查看plugin" \
        3 "返回" \
        2>&1 >/dev/tty)
case "${1}$choice" in
  1)lis1=$(ls $HOME/$name/plugins/example/ | sed 's#.*/##' | sed -e '/进群退群通知.js/d;/一言.js/d;/主动复读.js/d' | cat -n)&&lis2=($lis1)
dialog --clear --title "$title" --msgbox "$([ ! -z $lis2 ]&&echo $lis1| awk '{for (i=1; i<=NF; i++) {printf "%s ", $i; if (i%2==0) printf "\\n"}}' || echo '暂无插件')" 20 40;list;;
  2)yunzai list plugin;back;list;;
  3|*)home;;
esac
}

pluginlist(){
choice=$(dialog --clear --title "$title" --ok-button "确认" --cancel-button "返回" --menu "插件索引页" 180 40 2 $(arr=($(yunzai-list --all))
for ((i=0; i<${#arr[@]}; i++)); do
    echo -en "${arr[$i]}\n"
done | cat -n) 2>&1 >/dev/tty)
if [ ! $choice ];then
install
else
case "$choice" in
*)
int=($(yunzai-list --all))
int=${int[$chioce]}
esac
plist=($(yunzai-list $int))

choice=$(dialog --clear --title "$title" --yes-button "确认" --no-button "取消" --yesno "插件名称: ${plist[1]}\n仓库地址: ${plist[0]}\n确定安装$int吗？" 8 50 2>&1 >/dev/tty)
if [ $? = 0 ];then
clear;yunzai install -y $int;back;pluginlist
elif [ $? = 1 ];then
pluginlist
fi
fi
}

installs(){
choice="$(dialog --clear --title "$title" --ok-button "确认" --cancel-button "返回" --checklist "插件索引页\n请点击窗口选中，再次点击取消" 180 40 2 $(arr=($(yunzai-list --all))
for ((i=0; i<${#arr[@]}; i++)); do
    echo -en "${arr[$i]}\n"
done | cat -n | awk '{for (i=1; i<=NF; i++) {printf "%s ", $i; if (i%2==0) printf "off "}}') 2>&1 >/dev/tty)"
if [ ! ${choice} ];then
install
else
insl=" "
for ins in $choice; do
int=($(arr=($(yunzai-list --all))
for ((i=0; i<${#int[@]}; i++)); do
    echo -en "${int[$i]}\n"
done))
insl=("${insl[*]}" ${int[@]})
done
choice=$(dialog --clear --title "$title" --yes-button "确认" --no-button "取消" --yesno "- 所有选中的plugin名称:\n${insl[*]}\n\n- 选中的plugin数量: $(echo "${insl[*]}" | wc -w)\n\n确定安装这些吗？" 20 50 2>&1 >/dev/tty)
if [ $? = 0 ];then
clear;yunzai install -m -y ${insl[*]};back;installs
elif [ $? = 1 ];then
installs
fi
fi
}

install(){
choice=$(dialog --clear --title "$title" \
        --ok-button "确认" --cancel-button "返回" \
        --menu "项目安装" 20 33 2 \
        1 "检查索软件并安装" \
        2 "安装$name" \
        3 "查看可安装的plugin" \
        4 "从指定URL安装js插件" \
        5 "安装指定plugin" \
        6 "多选安装plugin" \
        7 "返回" \
        2>&1 >/dev/tty)
case "${1}$choice" in
  1)clear;yunzai install --add;back;install;;
  2)clear;yunzai install Yunzai;back;install;;
  3)clear;dialog --clear --title "$title" --msgbox "$(arr=($(yunzai-list --all))
for ((i=0; i<${#arr[@]}; i++)); do
    echo -en "${arr[$i]}\n"
done | cat -n)" 180 40;install;;
  4)installURL="$(inputbox "请输入插件网址:")"&&installNAME="$(inputbox "请输入插件名称(可留空):")"
if [ $? = 0 ];then
clear;yunzai install --custom -y "$installURL" "$installNAME";back;install
elif [ $? = 1 ];then
install
fi;;
  5)pluginlist;;
  6)installs;;
  7|*)home;;
esac
}

rmjsg(){
jsls=$(ls $HOME/$name/plugins/example/ | sed 's#.*/##' | sed -e '/进群退群通知.js/d;/一言.js/d;/主动复读.js/d' | cat -n)
if [ ! $(echo $jsls|wc -L) = 0 ];then
rmjs=$(dialog --clear --title "$title" --ok-button "确认" --cancel-button "返回" --menu "选择删除js插件" 180 40 2 $jsls 2>&1 >/dev/tty)
if [ $? = 1 ];then
rmplug
else
rmj=($jsls)
rmjs1=(${rmj[$((($rmjs * 2) - 1))]})
choice=$(dialog --clear --title "$title" --yes-button "确认" --no-button "取消" --yesno "确定删除$rmjs1吗？" 5 50 2>&1 >/dev/tty)
if [ $? = 0 ];then
clear;yunzai --remove --js $rmjs1;back;rmjsg
elif [ $? = 1 ];then
rmjsg
fi
fi
else
dialog --clear --title "$title" --msgbox "暂无js插件！" 7 40;rmplug
fi
}

rmpug(){
pls=$(ls $HOME/$name/plugins/ | sed -e '/example/d;/genshin/d;/other/d;/system/d;/adapter/d' | cat -n)
if [ ! $(echo $pls|wc -L) = 0 ];then
rmpu=$(dialog --clear --title "$title" --ok-button "确认" --cancel-button "返回" --menu "选择删除plugin" 180 40 2 $pls 2>&1 >/dev/tty)
if [ $? = 1 ];then
rmplug
else
rmp=($pls)
rmpu1=(${rmp[$((($rmpu * 2) - 1))]})
choice=$(dialog --clear --title "$title" --yes-button "确认" --no-button "取消" --yesno "确定删除$rmpu1吗？" 5 50 2>&1 >/dev/tty)
if [ $? = 0 ];then
clear;yunzai --remove $rmpu1;back;rmpug
elif [ $? = 1 ];then
rmpug
fi
fi
else
dialog --clear --title "$title" --msgbox "暂无plugin！" 7 40;rmplug
fi
}

rmplug(){
choice=$(dialog --clear --title "$title" \
        --ok-button "确认" --cancel-button "返回" \
        --menu "卸载插件" 16 30 2 \
        1 "卸载js插件" \
        2 "卸载plugin" \
        3 "返回" \
        2>&1 >/dev/tty)
case "${1}$choice" in
1)rmjsg;;
2)rmpug;;
3|*)remove;;
esac
}

remove(){
choice=$(dialog --clear --title "$title" \
        --ok-button "确认" --cancel-button "返回" \
        --menu "删除项目" 16 30 2 \
        1 "卸载$name" \
        2 "卸载指定插件" \
        3 "返回" \
        2>&1 >/dev/tty)
case "${1}$choice" in
1)clear;yunzai --remove Yunzai;back;remove;;
2)rmplug;;
3|*)home;;
esac
}

#主菜单
home(){
choice=$(dialog --clear --title "$title" \
        --ok-button "确认" --cancel-button "关闭" \
        --menu "主界面" 20 40 2 \
        1 "查看$name安装的插件" \
        2 "查看$name占用空间" \
        3 "启动$name" \
        4 "安装相关项目" \
        5 "删除项目" \
        6 "项目资源索引页" \
        7 "版本信息" \
        8 "退出" \
        2>&1 >/dev/tty)
case "${1}$choice" in
  1)list;;
  2)clear;yunzai --occupy;back;home;;
  3)clear;yunzai --start;back;home;;
  4)install;;
  5)remove;;
  6)dialog --clear --title "$title" --msgbox "Yunzai相关项目合集: \n$index" 7 60;home;;
  7)if [ -f $HOME/$name/package.json ];then
as="$name $(jq -r '.version' $HOME/$name/package.json || echo -e "\n版本未知\n未安装 jq")"
else
as='无'
fi
dialog --clear --title "$title" --msgbox "\n子系版本: v$versions\n\n子系更新时间: $times\n\n母体版本: v$version\n\n母体更新时间: $time\n\n正在管理的项目: $as" 20 40;home;;
  8|*)clear;return;;
esac
}

####激活主菜单函数后，待退出脚本后擦除函数
case $1 in
-v|version)echo -e "$times $versions";;
*)home;;
esac
unset $(grep -o '^[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*(){' $0 | tr -d '(){}')
trap - 2
